﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Configuration;

namespace WcfServiceHost
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class ServiceTest : IServiceTest
    {
        public bool UploadFile(Stream stream)
        {
            try
            {
                // Determine to which directory the file needs to be uploaded
                string uploadDirectory = @"C:\temp\upload";
                if (String.IsNullOrEmpty(uploadDirectory))
                {
                    throw new InvalidOperationException("...");
                }

                // Try to create the upload directory if it does not yet exist
                if (!Directory.Exists(uploadDirectory))
                {
                    Directory.CreateDirectory(uploadDirectory);
                }

                // Check if a file with the same filename is already 
                // present in the upload directory. If this is the case 
                // then delete this file
                string path = Path.Combine(uploadDirectory, "test.docx");
                if (File.Exists(path))
                {
                    // Check if the file is read-only
                    if ((File.GetAttributes(path) & FileAttributes.ReadOnly) ==
                        FileAttributes.ReadOnly)
                    {
                        File.SetAttributes(path, FileAttributes.Normal);
                    }
                    File.Delete(path);
                }

                // Read the incoming stream and save it to file
                const int bufferSize = 2048;
                byte[] buffer = new byte[bufferSize];
                using (FileStream outputStream = new FileStream(path,
                    FileMode.Create, FileAccess.Write))
                {
                    int bytesRead = stream.Read(buffer, 0, bufferSize);
                    while (bytesRead > 0)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        bytesRead = stream.Read(buffer, 0, bufferSize);
                    }
                    outputStream.Close();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
